package com.training1.core.modules.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.api.exception.ApiAuthenticationException;
import com.training1.api.login.APILoginUtil;
import com.training1.core.common.entity.UserEntity;
import com.training1.core.common.global.Global;
import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.IdGen;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.common.utils.excel.ExcelUtil;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.dao.MemberDao;
import com.training1.core.modules.dao.SystemSettingsDao;
import com.training1.core.modules.dao.UserDao;

@Service
@Transactional
public class MemberAccountService extends CrudService<MemberDao,MemberEntity>{
	
	
	@Resource
	private MemberDao memberDao;
	
	private static final Logger log = LoggerFactory.getLogger(MemberAccountService.class);

	@Value("${api.debug}")
	private String debug;
	
	@Resource
	private SystemSettingsService systemSettings;
	
	/**
	 * 获得会员账户信息
	 * @param param
	 * @return
	 */
	public List<MemberEntity> findNodeAccount(Map<String, Object> param){
		return dao.findAccountList(param);
	}
	
	@Resource
	@Value(value="${apiExpiration}")
	private String expiration;
	
	public void export(HttpServletResponse response,Map<String, Object> param) {
		List<MemberEntity> memList = memberDao.findAccountList(param);//根据dao中的内容用方法
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("会员姓名");
		excelHead.add("会员手机号");
		excelHead.add("开单中心");
		excelHead.add("总收益");
		excelHead.add("余额");
		excelHead.add("乐花");
		excelHead.add("贴心宝");
		excelHead.add("税费");
		excelHead.add("充值金额");
		excelHead.add("推荐奖");
		excelHead.add("A1奖");
		excelHead.add("A2奖");
		excelHead.add("A3奖");
		excelHead.add("B1奖");
		excelHead.add("B2奖");
		excelHead.add("B3奖");
		excelHead.add("死点返还");
		
		//设置内容
		List<List<String>> dataList = new ArrayList<List<String>>();
		for(MemberEntity mem : memList){
			List<String> data = new ArrayList<String>();
			data.add(mem.getMemberName());
			data.add(mem.getCellPhone());
			data.add(mem.getUserName());
			if(mem.getTotalIncome() != null){
				data.add(mem.getTotalIncome().toString());
			}else{
				data.add("0");
			}
			if(mem.getAvaibleCash() != null){
				data.add(mem.getAvaibleCash().toString());
			}else{
				data.add("0");
			}
			if(mem.getCusumeCash() != null){
				data.add(mem.getCusumeCash().toString());
			}else{
				data.add("0");
			}
			if(mem.getInvestmentCash() != null){
				data.add(mem.getInvestmentCash().toString());
			}else{
				data.add("0");
			}
			if(mem.getTax() != null){
				data.add(mem.getTax().toString());
			}else{
				data.add("0");
			}
			data.add(mem.getRechagreCash());
			data.add(mem.getRecommendAward());
			data.add(mem.getA1Award());
			data.add(mem.getA2Award());
			data.add(mem.getA3Award());
			data.add(mem.getB1Award());
			data.add(mem.getB2Award());
			data.add(mem.getB3Award());
			data.add(mem.getDpBackAward());
			
			dataList.add(data);
		}
		
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "会员账户表_"+DateUtils.getDate().replace("-", "");
		titleName = "会员账户表_"+DateUtils.getDate().replace("-", "");
		
		ExcelUtil.exportExcel(response, excelHead, dataList, fileName, sheetName,titleName);
	}
	
	public MemberEntity findSUMList(Map<String,Object> param) {
		return dao.findSUMList(param);
	}
}
